//
//  HWDynamicListCell.swift
//  HWanGame
//
//  Created by Apple on 2024/8/15.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class HWDynamicListCell: UITableViewCell {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20*widthScale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(11.font_medium).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium)
        .line(0).end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private let browseImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_look").end()
    private let commitImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_commit").end()
    private let likeImageView = c_make_ex(UIButton.self)
        .image("WuKong_like").image("Detail_like_sel", state: .selected).end()
    private let browseLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).text("999浏览").end()
    private let commitLikeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).alignment(.right).end()
    
    var circleModel:HWGameCircleModel? {
        didSet{
            guard let md = circleModel else { return }
            
            iconImageView.sd_setImage(with: URL(string: md.face), completed: nil)
            nameLabel.text = md.pet_name
            contentLabel.text = md.content
            timeLabel.text = md.time
            browseLabel.text = md.look + "浏览"
            commitLikeLabel.text = md.pinglun + "评论 · " + md.zan + "点赞"
            likeImageView.isSelected = md.is_zan
            if md.video.count > 0 {
                collectionView.heightConstraint?.constant = 152*widthScale
            }else if md.img.count == 1 {
                collectionView.heightConstraint?.constant = 152*widthScale
            }else if md.img.count == 2 {
                collectionView.heightConstraint?.constant = 85*widthScale
            }else if md.img.count > 2 {
                let index = (md.img.count + 2) / 3
                collectionView.heightConstraint?.constant = (CGFloat.screenWidth - 110.0*widthScale)/3 * CGFloat(index)
            }else{
                collectionView.heightConstraint?.constant = 0
            }
            collectionView.reloadData()
        }
    }
    
    var zanButtonClick:((_:String)->())?
    var jumpToDetail:((_:HWGameCircleModel)->())?
    /// 点击图片
    var didSelectImage:((_:Int,_:[String])->())?
    /// 点击视频
    var didSelectVideo:((_:String)->())?
    
    var model: HWWuKongDataModel?{
        didSet{
            guard let md = model?.data else { return }
            
            circleModel = md
        }
    }
    
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                iconImageView
                nameLabel
                timeLabel
                contentLabel
                collectionView
                browseImageView
                commitImageView
                likeImageView
                browseLabel
                commitLikeLabel
            }
        }
        
        backView.leading(12*widthScale).trailing(12*widthScale).top(0).bottom(12*widthScale)
        iconImageView.leading(12*widthScale).top(16*widthScale).size(40*widthScale)
        nameLabel.leading(64*widthScale).top(20*widthScale).trailing(65*widthScale)
        timeLabel.leading(64*widthScale).top(40*widthScale).trailing(65*widthScale)
        contentLabel.leading(64*widthScale).top(64*widthScale).trailing(16*widthScale)
        collectionView.leading(0).trailing(0).height(0)
        collectionView.Top == contentLabel.Bottom + 8*widthScale
        browseImageView.leading(52*widthScale).size(44*widthScale)
        browseImageView.Top == collectionView.Bottom
        likeImageView.trailing(4*widthScale).size(44*widthScale)
        likeImageView.CenterY == browseImageView.CenterY
        commitImageView.size(44*widthScale)
        commitImageView.CenterY == likeImageView.CenterY
        commitImageView.Trailing == likeImageView.Leading
        browseLabel.leading(64*widthScale).bottom(15*widthScale)
        browseLabel.Top == browseImageView.Bottom
        commitLikeLabel.trailing(26*widthScale)
        commitLikeLabel.CenterY == browseLabel.CenterY
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 2*widthScale
        layout.minimumInteritemSpacing = 2*widthScale
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 0, left: 64*widthScale, bottom: 0, right: 15*widthScale)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWGameCircleCell.self, forCellWithReuseIdentifier: HWGameCircleCell.description())
        collectionView.showsVerticalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
                
        let tapG = UITapGestureRecognizer.init(target: self, action: #selector(commentButtonClick))
        commitImageView.isUserInteractionEnabled = true
        commitImageView.addGestureRecognizer(tapG)
        likeImageView.addTarget(self, action: #selector(likeButtonClick), for: .touchUpInside)
    }

    @objc private func commentButtonClick() {
        jumpToDetail!(circleModel ?? HWGameCircleModel())
    }
    
    @objc private func likeButtonClick(){
        zanButtonClick!(circleModel?.id ?? "")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWDynamicListCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if circleModel?.video.count ?? 0 > 0 {
            return 1
        }else{
            return circleModel?.img.count ?? 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let pageCell = collectionView.dequeueReusableCell(withReuseIdentifier: HWGameCircleCell.description(), for: indexPath) as? HWGameCircleCell else { return UICollectionViewCell() }
        pageCell.model = circleModel
        if circleModel?.video.count ?? 0 > 0{
            
        }else{
            
            pageCell.imageView.sd_setImage(with: URL(string: circleModel?.img[indexPath.row] ?? ""), completed: nil)
        }
        pageCell.layoutIfNeeded()
        if circleModel?.img.count ?? 0 == 1 {
            pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 8*widthScale)
        }else if circleModel?.img.count ?? 0 == 2 {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft], radius: 8*widthScale)
            }else {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else if circleModel?.img.count ?? 0 == 3 {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight,.bottomRight], radius: 8*widthScale)
            }else {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else if circleModel?.img.count ?? 0 == 4 || circleModel?.img.count ?? 0 == 5{
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 3 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
            
        }else if circleModel?.img.count ?? 0 == 6 {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 3 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else if indexPath.row == 5 {
                pageCell.imageView.addCorner(conrners: [.bottomRight], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else if circleModel?.img.count ?? 0 == 7 || circleModel?.img.count ?? 0 == 8{
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 6 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 6 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else if indexPath.row == 8 {
                pageCell.imageView.addCorner(conrners: [.bottomRight], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }
        
        return pageCell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if circleModel?.video.count ?? 0 > 0 {
            return CGSize (width: CGFloat.screenWidth - 104*widthScale, height: 152*widthScale)
        }else if circleModel?.img.count ?? 0 == 1 {
            return CGSize (width: CGFloat.screenWidth - 104*widthScale, height: 152*widthScale)
        }else if circleModel?.img.count ?? 0 == 2{
            
            return CGSize (width: (CGFloat.screenWidth - 106.0*widthScale)/2, height: 85*widthScale)
        }else{
            
            return CGSize (width: (CGFloat.screenWidth - 110.0*widthScale)/3, height: (CGFloat.screenWidth - 110.0*widthScale)/3)
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if circleModel?.video.count ?? 0 > 0 {
            didSelectVideo?((circleModel?.video)!)
        }else{
            didSelectImage?(indexPath.row,circleModel?.img ?? [])
        }
    }
}
